package com.fyzk.activity.fragment;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.fyzk.activity.lottery.recommend_order.MyRecommendOrderBean;
import com.fyzk.activity.lottery.recommend_order.MyRecommendOrderUserBean;
import com.fyzk.adapter.DocumentaryUserAdapter;
import com.fyzk.base.BaseFragment;
import com.fyzk.model.BluePrintDetailsViewModel;
import com.fyzk.net.Commands;
import com.fyzk.utils.IntentParamUtils;
import com.fyzk.view.widget.CustomLoadMoreView;
import com.fyzk.view.widget.DividerItemDecoration;
import com.tyjc.lottery.R;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.List;

import http.Common.CommandUtil;
import http.DataUtil.text.TextUtil;
import http.StringResultBean;

/**
 * 跟单用户
 */
@ContentView(R.layout.fragment_documentary_user)
public class DocumentaryUserFragment extends BaseFragment implements  Observer<MyRecommendOrderBean> {
    @ViewInject(R.id.rv_documentary_user)
    RecyclerView mRvDocumentaryUser;

    private MyRecommendOrderBean mInfo;
    private BluePrintDetailsViewModel mBluePrintDetailsViewModel;

    public static DocumentaryUserFragment newInstance(MyRecommendOrderBean mInfo) {

        Bundle args = new Bundle();

        args.putSerializable(IntentParamUtils.RECOMMENDINFOR, mInfo);
        DocumentaryUserFragment fragment = new DocumentaryUserFragment();
        fragment.setArguments(args);
        return fragment;
    }

    private DocumentaryUserAdapter mAdapter;
    private LinearLayoutManager mDocumentaryUserManager;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        if (null != getArguments()) {
            mInfo = (MyRecommendOrderBean) getArguments().getSerializable(IntentParamUtils.RECOMMENDINFOR);
        }
        return x.view().inject(this, inflater, container);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initViews();
    }

    private void initViews() {
        mDocumentaryUserManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
        mAdapter = new DocumentaryUserAdapter();
        mRvDocumentaryUser.setLayoutManager(mDocumentaryUserManager);
        mRvDocumentaryUser.setAdapter(mAdapter);
        mBluePrintDetailsViewModel = ViewModelProviders.of(getActivity()).get(BluePrintDetailsViewModel.class);
        mBluePrintDetailsViewModel.getBluePrintDetailsViewModel().observe(this, this);
        loadData(mInfo.getRecommendId(), false);
    }

    /**
     * 跟单用户
     *
     * @param recommendId  请求id
     * @param showProgress 是否显示loading
     */
    private void loadData(String recommendId, boolean showProgress) {
        JSONObject request_json = new JSONObject();
        try {
            request_json.put("recommendId", recommendId);    // 类型（荐单：0；跟单：1）
            request_json.put("startLine", 0);
            request_json.put("maxLine", 20);
            httpPostJson(CommandUtil.FINDFOLLOWERSLIST, Commands.FINDFOLLOWERSLIST, request_json,
                    showProgress);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onSuccess(int requestCode, StringResultBean resultBean, String result) {
        super.onSuccess(requestCode, resultBean, result);
        List<MyRecommendOrderUserBean> mData = JSON.parseArray(resultBean.getData(), MyRecommendOrderUserBean.class);
        mAdapter.setNewData(mData);
        if (null == mData || mData.size() == 0) {
            mAdapter.setEmptyView(R.layout.layout_end_of_video_rv, mRvDocumentaryUser);
        }
    }

    @Override
    protected void onFailure(int requestCode, String message) {
        super.onFailure(requestCode, message);
        mAdapter.setNewData(null);
        mAdapter.setEmptyView(R.layout.layout_end_of_video_rv, mRvDocumentaryUser);
    }

    @Override
    public void onFailure(int requestCode, String errorcode, String msg) {
        super.onFailure(requestCode, errorcode, msg);
        mAdapter.setNewData(null);
        mAdapter.setEmptyView(R.layout.layout_end_of_video_rv, mRvDocumentaryUser);
    }

    @Override
    public void onChanged(@Nullable MyRecommendOrderBean data) {
        loadData(data.getRecommendId(), false);
    }
}
